/*-----------------------------------------------------------------------------
File: ProcessInput.cpp

History:
02/28/11  jnr   spun processing modules off into their own .cpp/.h files
-----------------------------------------------------------------------------*/
#include "stdafx.h"
#include "ProcessInput.h"
#include "globals.h"


vector<Comment *> * allComments;

bool processTag(string filename, ifstream &stream, string tag, string currLine)
{
	if(currLine.find(tag) == string::npos)
		return false; //no tag found in this line
	bool flag = true;
	vector<string> * comm = new vector<string>();            // list comment of each comment
	Comment *c = new Comment();
	comm->push_back(tag);                   
	string line;
	while (flag)
	{          // read all comments under author until the @author
		getline(stream, line);
		if ( !(line.find(tag)<line.length()) )
		{
			if (line.find("//")<line.length()) // remove // before each comment if there
				  line = line.substr(2,line.length()-2); // line.erase(0,1);
			// write the first comment that belongs to @author 
			comm->push_back(line);
		 }
		 else
		 {
			flag=false;
		 }
	 }         
	 c->setComments(filename,comm);     
	 allComments->push_back(c); 
	 return true;
}
// coded by Cuong Tran : 02/23/2011
void read_comments ( char *pathToProject, WIN32_FIND_DATA fd )
{
	char temp[MAX_PATH];
	sprintf(temp,"%s\\%s",pathToProject,fd.cFileName);
	
	std::ifstream Source;        // input files.
	
	Source.open ( temp );
	if(!Source)
	{
		MessageBoxA(0, (LPCSTR)"Cannot open file", (LPCSTR)"cplusplusdoc",MB_OK);
		return;
	}
	string line,filename;    
   
    filename = (char *)fd.cFileName;

	while ( ! Source.eof() ) // whole input file is to read.
	{
		  getline(Source, line); // reading line by line.
	  
		  //NOTE: potential problem if the user has their comments on a single
		  //      line, like the following:
		  //      /*@<tag>  [info here...] @<tag>*/
		  //                   - or -
		  //      //@<tag>  [info here...] @<tag>
		  		  
		  for(int i=0; i<NUM_SUPPORTED_TAGS; i++)
			processTag(filename, Source, SupportedTagArray[i],line);
	}
	Source.close(); // close file.
}